package com.yazi.mxz.job.quartz;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


import com.myframework.smhj.common.tool.DateUtil;
import com.yazi.mxz.common.entity.beans.Order;
import com.yazi.mxz.common.service.OrderService;

/**
 * 订单自动确认收货进程
 * @author mo_yq5
 * @date 2015-1-8
 */
public class OrderSuccessJob extends MyJob {
	private static Logger log = LoggerFactory.getLogger(OrderSuccessJob.class);
	private static Long orderId = null;
	private static Long nextTime = null;
	@Resource
	OrderService orderService;
	
	@Override
	public void run() {
		Order order = null;
		if (null != orderId) {
			orderService.editToSuccess(orderId);
		}
		order = orderService.oneNextSuccess();
		if (null == order) {
			nextTime = null;
			orderId = null;
			return;
		}
		orderId = order.getOrderId();
		nextTime = order.getStatusTime() + 3600000 * 24;// TODO 24小时自动确认订单
		long curTimeStamp = DateUtil.getTimeStamp();
		if (nextTime > curTimeStamp) {
			try {
				Thread.sleep(nextTime - curTimeStamp);
			} catch (InterruptedException e) {
				log.info(e.getMessage());
			}			
		}
		run();
	}

	@Override
	public void checkDoit(long timeStamp) {
		if (null == nextTime) {
			doit();
		}
	}
	
	public void checkDoit() {
		checkDoit(0);
	}

}
